# Smart Fan for KidsIOT
## 1. Code
[DOWNLOAD](../Code.zip)
Download and unzip these files. Here all codes are in folder **2.Code_kidsIOT**.
For convenience, we move the codes into: **D:\Code\2.Code_kidsIOT**. You can also choose to move it into any disks at will.
---
## 2. Development Environment Configuration
### 2.1 KidsBlock Download

1. [KidsBlock Download](https://wiki.kidsbits.cc/projects/KidsBlock/en/latest/download/)
2. Installation
- [Windows System](https://wiki.kidsbits.cc/projects/KidsBlock/en/latest/Windows/)
- [MacOS System](https://wiki.kidsbits.cc/projects/KidsBlock/en/latest/MacOS/)
3. [Driver Installation](https://wiki.kidsbits.cc/projects/KidsBlock/en/latest/driver/)
---
### 2.2 KidsBlock Tutorial
1. Make sure the board is connected to computer. Open KidsBlock and choose a device.

Choose **kidsIOT**.

Click **Connect**.

**Go to Editor**.


2. Build code blocks and upload.
**Method ①**: Directly drag blocks to the editing area.

After building your blocks, save it to your computer: **File --> Save to your computer**

Click  to upload the code.

**Method ②**: Load code from your computer.
Download code in **1. Code** to your computer. For convenience, here we save it to D:\Code\2.Code_kidsIOT.
**File --> Load from your computer** and choose code to open.

After loading code, connect to the corresponding port.


After that, click  to upload code.

**Main Interface**


---
## 3. Modules
Please move the codes to a convenient path as your needs, for instance, path: **D:\Code\2.Code_kidsIOT**.
### KidsIOT Ports View
During experiments, modules can only be connected to ports in the same color.

### 3.1 White LED Module


**LED (Light-Emitting Diode)**
LED is a commonly used light emitting device that converts electrical energy into light energy. Usually, it is used as an indicator in circuits and instruments, or as part of texts or numeric display.
It generally includes gallium(Ga), arsenic(As), phosphorus(P), nitrogen(N) and so on.
| LED components | Emitting light colors |
| :---------------------: | :-------------------: |
| gallium arsenide diode | red |
| gallium phosphide diode | green |
| silicon carbide diode | yellow |
| gallium nitride diode | blue |

#### Parameters

Operating voltage: DC 3.3 ~ 5 V
Operating current: 1.5 mA (Peak: 2.3mA)
Maximum power: 0.07 W
Control signal: digital signal
Dimensions: 24 x 48 x 18 mm (without housing)
Positioning holes: diameter of 4.8 mm
Interface: telephone socket

#### Principles

Modules with blue housing are digital ones, so we should connect to digital io pins of the mainboard (ports with blue).

In this experiment, we connect the white LED module to port 1. According to the board ports view, the digital io pin at port 1 is digital pin io13.
When we set the pin to high(1), the LED lights up in white; if we set to low(0), it will be off.

#### Wiring Diagram

#### Test Code
Open KidsBlock and connect to the board, click File --> Load from your computer.

Choose D:\Code\2.Code_kidsIOT and open **3.1Light_on.sb3**.

Click  to connect to COM port and then .
#### Explanation

**Code Blocks**
| Blocks | Code block |
| :---------------------------: | :---------------------------: |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |

**Conceive**
1. **Initialization**
Set pin mode.

**Build blocks:**
① Drag  and  to the editing area.
② Click the triangle to choose pin IO13.

③ Modify the mode into output.
Three pin mode:
- input
- output
- input-pullup

Q :Why output?
A :The code is written for the mainboard. For the board, pin io13 is outputting power levels (high or low) to the connected module.


2. **Main Code**
Loop: LED lights on for 1s and goes off for 1s, in a loop.

**Build blocks:**
① Drag  .This is a loop code, in which codes will run forever.
② Drag  into "forever".
There are two power level to output:
- high
- low
Choose pin to IO13 and output high.
③ Put  into "forever". This is a delay code.

Q :Why delay 1s?
A :If you output a high level to LED, it will be always on. Yet, we add a delay of 1s, so it lights up for only 1s. Delay time is the ON/OFF time of LED.

Pin IO13 outputs high for 1s:

④ Right-click the code and choose "Duplicate".

As follows:

Set the output to low. Pin IO13 outputs low for 1s.

LED will lights on for 1s and goes off for 1s, in a loop.

#### Test Result

After uploading code, the LED module will flash with an interval of 1s (on for 1s and off for 1s).


---
### 3.2 RGB LED Module (Common Anode)


RGB LED is imaged in the intersection of three primary colors (RGB): red, green and blue. Both white LED and RGB LED are able to emit white light. The former is presented directly in white, while the latter is mixed with red, green and blue.
**Trichromatic Theory**

Human eyes are sensitive to RGB colors. Most colors can be synthesized by RGB in different proportions. Therefore, the vast majority of monochromatic light can also be decomposed into RGB colors. This is the most basic principle of colorimetry --- trichromatic theory.
Red, green and blue lights are called additive primary colors because by the combination of these three primaries in different proportion, various colored lights will produce. Similarly, there are also subtractive ones. So we may add or/and subtract colors as needed.
The three primary colors of the paint can not compose white, yet, with optical elements, those of light can do it, which is mixed by the three equal parts of RGB.

#### Parameters

Operating voltage: DC 3.3 ~ 5V
Current: 19.5 mA
Maximum power: 0.975 W
Operating temperature: -10°C ~ +50°C
Dimensions: 24 x 48 x 18 mm (without housing)
Positioning holes: diameter of 4.8 mm
Interface: telephone socket

#### Principle

This RGB LED integrates 3 LED inside: red, green and blue.
This RGB LED diode is designed with 4 pins, among which three control LED pins of R, G and B, and one is common anode, which need to be connect to power positive.
When using, we need to connect this module to the white ports of the mainboard.
In this experiment, we connect the RGB LED module to white port 1. According to the board ports view, R pin is connect to io12, G to io13 and B to io14.
When we set the pins to high(1), the LED lights up in corresponding color; if we set to low(0), it will be off.

#### Wiring Diagram

#### Test Code
Open KidsBlock and connect the board to computer. Choose **File --> Load from your computer**.

Choose D:\Code\2.Code_kidsIOT and open **3.2RGB.sb3**

Click  to connect to port and then .
#### Explanations

**Code Blocks**
| Blocks | Code block |
| :---------------------------: | :---------------------------: |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |

**Conceive:**
The framework of the code is similar to the previous project, so you may have a reference.
1. **Initialization**
Set pin mode.


2. **Main code**
Loop: LED lights up in red, green, blue and white for 1s each.


#### Test Result

After uploading code, the RGB LED will circulate to light up in red, green, blue and white respectively, each color for 1s, in a circulation.


---
### 3.3 Photo Interrupter


Photo interrupter works based on photoresistor and light source.
Generally, this module is composed of two photoelectric components: light source and the light receiver. The light source is usually an LED, while the light receiver consists of a photosensitive element and a signal amplifier. When light is blocked, the photosensitive element receives no light, so its resistance value changes. This change will be converted into an electrical signal and then processed.

#### Parameters

Operating voltage: DC 3.3 ~ 5V
Operating current: 24 mA
Maximum power: 0.12 W
Operating temperature: -10°C ~ +50°C
Control signal: digital signal
Dimensions: 24 x 32 x 18 mm (without housing)
Positioning holes: diameter of 4.8 mm
Interface: telephone socket

#### Principle

Modules with blue housing are digital ones, so we should connect to digital io pins of the mainboard (ports with blue).
In this experiment, we connect the module to port 3. According to the board ports view, the digital pin at port 3 is io26.
When objects block lights, the module outputs high; if not, it outputs low.

#### Wiring Diagram

#### Test Code
Open KidsBlock and connect the board to computer. Choose **File --> Load from your computer**.

Choose D:\Code\2.Code_kidsIOT and open **3.3Photo_Interrupt.sb3**.

Click  to connect to port and then .
#### Explanations

**Code Blocks**
| Blocks | Code block |
| :-------------------------------: | :-------------------------------------: |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |

**Conceive:**
1. **Initialization**
Set pin mode and serial monitor first.

**Build blocks:**
① Drag ,  and  to the editing area as above.
 is used to set baud rate and initialize serial monitor. If you need a serial port in an experiment, this code block will be necessary, or else the monitor may fails to print results.
② Drag  to the editing area and duplicate it twice, and set them as shown above.
This block will define a new variable whose range(Global or Local), type and initial value can be set.

2. **Main code:**
Loop: Cumulatively read the values of the module and print them.

**Build blocks:**
① Drag , ,  and  to the editing area, and place them as follows:

The module is connect to IO26 on the board so we set to read IO26 pin value and assign it to State.

Q:What role does the delay play here?
Delay 0.1s to read values.
If we do not set a delay, the reading speed will be very fast so that the outputs may be inaccurate. As follows, more than one values will output although we only perform the action once.

A:We add delay to stabilize the value read by IO26.

②  is an operation that it determines whether the two values equal each other. If yes, the condition is True.
 denies the condition in it.
 is a variable.
Drag , ,  to the editing area and put them as follows:

Place it in the condition of .

It means: the value of variable State **≠** the value of variable lastState.
③  determines whether the condition is true. If it is, codes in it will be executed. If not, codes in it will be skipped.
Put  in "forever".
Combine them as follows:

These blocks reads the value of D3 and assigns it to variable State, and then it determines whether State ≠ lastState.
④ Build , ,  as follows:

Then put this combination into the "if" in ③.
In these code, a **nested-if structure** is adopted: an **if statement** in another **if**.

These blocks reads the value of IO26 and assigns it to State, and then it determines whether State ≠ lastState.
- State ≠ lastState: execute the second "if" block and determine whether State =1.
- State = lastState: skip.
④  is an operation whose result is the sum of these two values.
Drag  into the editing area.
Set the left one to PushCounter and the right one to 1:

Place ,  and  as follows:

In these code, a **nested-if structure** is adopted: an **if statement** in another **if**. Let's have a look at the operational logic diagram:
⑤ Put  into "forever" and name the variable to lastState, and assign the value of State to it.

① Assign the value output by the photo interrupter to State
② Determine whether State ≠ lastState
- State ≠ lastState: execute the next if statement
- State = lastState: exit if loop to execute ⑤
③ (Precondition: State ≠ lastState) Determine whether State = 1
- State = 1: PushCounter +1 and output it.
- State ≠ 1: exit if loop.
④ (Precondition: State ≠ lastState, State = 1) PushCounter +1 and print the value on Shell.
⑤ Assign the value of State to lastState
Initial Settings:
| Initial state | |
| ------------------------------------------------------------ | -------- |
| State: the present value output by the sensor signal terminal | set to 0 |
| lastState: the last value output by the sensor signal terminal | set to 0 |
| PushCounter: the number of times of light being interrupted | set to 0 |
Execution:
| State | |
| ------------------------------------------ | ------------------------------------------------------------ |
| the object passes through the groove once | the initial value of lastState is 0; State detects object so its value is 1.
State ≠ lastState, State = 1
PushCounter+1, and output the value.
lastState = State, lastState = 1 |
| the object leaves from the groove | lastState = 1; State detects no object so its value is 0.
State ≠ lastState, State = 0
lastState = State, lastState = 0 |
| the object passes through the groove again | lastState = 0; State detects object so its value is 1.
State ≠ lastState, State = 1
PushCounter+1, and output the value.
lastState = 1 |
| the object leaves from the groove | lastState = 1; State detects no object so its value is 0.
State ≠ lastState, State = 0
lastState = 0 |

#### Test Result

NOTE: Before uploading code, please set baud rate first to avoid garbled words.
Click  and set Buadrate to 115200.

After uploading code, place an opaque object in in the sensor groove and then remove it, so the light will be interrupted once. The serial monitor will print the number of times of light being interrupted.


---
### 3.4 Capacitive Touch Sensor


On the capacitive touch sensor, a touch switch is reserved to enable switch function through its variable area.
After powering on, the sensor requires about 0.5 seconds to stabilize itself. During this time, please do not touch it. Its all functions are disabled due to a self-calibration, whose cycle is about 4 seconds.

#### Parameters

Operating voltage: DC 3.3 ~ 5V
Operating current: 3 mA
Maximum power: 0.015 W
Operating temperature: -10°C ~ +50°C
Input signal: digital signal
Dimensions: 24 x 48 x 18 mm (without housing)
Positioning holes: diameter of 4.8 mm
Interface: telephone socket

#### Principle

Modules with blue housing are digital ones, so we should connect to digital io pins of the mainboard (ports with blue).
In this experiment, we connect the module to port 4. According to the board ports view, the digital io pin at port 4 is io27.
When we touch the capacitive area on the sensor, the sensor outputs high(1) and the on-board red LED lights up; when we release, it outputs low(0) and the LED goes off.

#### Wiring Diagram

#### Test Code
Open KidsBlock and connect the board to computer. Choose **File --> Load from your computer**.

Choose D:\Code\2.Code_kidsIOT and open **3.4Touch.sb3**.

Click  to connect to port and then .
#### Explanations

**Code Blocks**
| Blocks | Code block |
| :-------------------------------: | :-----------------------------------: |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |

**Conceive:**
1. **Initialization**
Set pin mode and initialize serial port.


2. **Main Code**
Loop: determine whether the value of pin IO27 equals 1.
- the value = 1: the module is touched and the serial monitor prints *You pressed the button !*
- the value ≠ 1: the module is not touched and the serial monitor prints *You loosen the button !*
Finally, add a delay of 0.1s: refresh the outputs every 0.1s.


#### Test Result

NOTE: Before uploading code, please set baud rate first to avoid garbled words.
Click  and set Buadrate to 115200.

After uploading code, touch the capacitive area on the sensor, and the monitor will print *You pressed the button!*. Release the touch switch, and it prints *You loosen the button!* .


---
### 3.5 TM1650 4-digit Tube Display


The module integrates 0.36-inch common cathode 4-digit tube display which is in red when lighting up. Its driver chip is TM1650, a chip for LED drive control special circuit. When using, we only need 2 signal wires to control the digital tube.

#### Parameters

Operating voltage: DC 3.3 ~ 5V
Operating current: 42 mA
Maximum power: 0.21 W
Operating temperature: -10°C ~ +50°C
Dimensions: 24 x 48 x 18 mm (without housing)
Positioning holes: diameter of 4.8 mm
Interface: telephone socket

#### Principle

TM1650 adopts a two-wire serial transmission protocol (not the standard I2C protocol). The chip communicates with MCU to drive digital tube only by two pins, which greatly saves pins.
During using, this module needs to be connected to the ports in green on th board.
However, first of all, we need to import the TM1650 library.

#### Wiring Diagram

#### Test Code
Open KidsBlock and connect the board to computer. Choose **File --> Load from your computer**.

Choose D:\Code\2.Code_kidsIOT and open **3.5Four-digit numeral tube.sb3**.

Click  to connect to port and then .
#### Explanations

**Code Blocks**
| Blocks | Code block |
| :-------------------------------------------: | :---------------------------------------------------------: |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |

**Conceive**
1. **Initialization**
Initialize the digit tube first. Define an integer variable i with an initial value of 0.

**Build blocks:**
① Click  and choose Display, find and click **Four Digital Tube TM1650_IIC** to add this module.

Click Back.

This module is successfully added.

② 
③ Add  under the "begin" block.

This block initializes the TM1650 four-digit tube module. Three modes are optional: clear, ON, OFF.

④ Duplicate .

As follows:

Modify its mode to ON to turn on this module.

⑤ Put  below and name the variable to i.

2. **Main Code**
Loop: display 0 ~ 999 in order, refresh the display every 0.01s.

**Build blocks:**
① 
② Put  in the "forever".
③ Find  and put  into the left of  and name the variable to i; set the value on the right to 999.

These blocks mean variable i < 999.
Put them in "if else" to set as a condition.

④ Place  under if. This block sets the module to display messages and number strings. By default, the message is abcd.
For this module, the displayed content must be in string form, so we cannot directly put the variable block into it. We need convert the variable to a string so that it can be shown on the tube.

Put  into the "display" block and set the output type. Three types are optional: integer, decimal, string.

Duplicate the variable i and put it into the "display" block.


As follows:

Modify the type into string.

These blocks will convert the variable i to string form, and display on the 4-digit tube.
⑤ Add  in if and name the variable to i. This block enables a variable to automatically increase or decrease.
++: automatically increase 1
--: automatically decrease 1

We have learned a similar code in chapter 3.3, which also make the variable to increase 1: 

⑥ Put  under else and modify the variable name to i. It sets i to 0.

**Examples:**
In initialization, we set the initial value of i to 0.
In first loop, i = 0, so i < 999, the value 0 will be converted into a string 0 to display on the tube. Next, i++ executes so i = 1.
In second loop, i = 1, so i < 999, the value 1 will be converted into a string 1 to display on the tube. Next, i++ executes so i = 2.
... ...
In the thousandth loop, i = 999, at this time i is not lower than 999, so i is set to 0.
In the loop of number thousand and one, i = 0, the value 0 will be converted into a string 0 to display on the tube. Next, i++ executes so i = 1.
... ...
So then the displayed number will circulate within 0 ~ 999
⑦ Add a delay  and set to 0.01s.
Therefore, numbers on the tube will be refreshed every 0.01s.


#### Test Result

After uploading code, the module displays number from 0 to 999, and the results refreshes every 0.01s.


---
### 3.6 DS18B20 Temperature Sensor


The DS18B20 sensor is a digital module used to detect temperature. It outputs digital signal and features small size, strong anti-interference ability and high precision.

#### Parameters

Operating voltage: DC 3.3 ~ 5V
Current: 0.15 mA
Maximum power: 0.00075 W
Temperature detection range: -55°C ~ +125°C (accuracy of ±0.4°C within -10°C ~ +70°C)
Operating temperature: -10°C ~ +50°C
Dimensions: 24 x 32 x 18 mm (without housing)
Positioning holes: diameter of 4.8 mm
Interface: telephone socket

#### Principle

Modules with blue housing are digital ones, so we should connect to digital io pins of the mainboard (ports with blue).
In this experiment, we connect the module to port 6. According to the board ports view, the digital io pin at port 6 is io23.

#### Wiring Diagram

#### Test Code
Open KidsBlock and connect the board to computer. Choose **File --> Load from your computer**.

Choose D:\Code\2.Code_kidsIOT and open **3.6Temperature.sb3**.

Click  to connect to port and then .
#### Explanations

**Code Blocks**
| Blocks | Code block |
| :---------------------------: | :-----------------------------------: |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |

**Build blocks:**
Click  to add library **18b20**.

1. **Initialization**
Initialize the serial port.


2. **Main Code**
Loop: Read the temperature value detected by DS18B20 sensor and print it without wrapping.

**Build blocks:**
① Drag ,  and  to the editing area and place them as the following:

These blocks will print the the temperature value detected by pin IO23 (DS18B20 temperature sensor) on the serial monitor without wrapping. The value is in the unit of °C, or you can change it to F in the code.

Q :Why outputting the temperature value without wrapping?
No wrap: these results will be output in the same line.
Wrap: these results will be output in the next line.
If we set to wrap to output, the unit °C and the temperature value cannot be displayed on the same line.
Wrap:

No wrap:

A :We set to output without wrapping to make the value and °C in the same line.

② Drag  into "forever" and set the print message to " °C".
There are two space before °C.


Q :Why two space?
If here is no space, the unit °C will be very close to the value.
No space:

With spaces:

A :For convenient to check results, we add two spaces before °C.


#### Test Result

NOTE: Before uploading code, please set baud rate first to avoid garbled words.
Click  to set Buadrate to 115200.

After uploading code, the serial monitor prints the temperature value detected by DS18B20 sensor.


---
### 3.7 Ultrasonic Sensor


When bats fly, they produce ultrasonic waves from their throats, which are emitted from their mouths. When ultrasonic waves encounter an insect or obstacle, they are reflected back and then picked up by the bats' ears. Bats calculate the distance by the reflected wave and also determine whether the target is an insect or an obstacle.
Therefore, scientists developed ultrasonic ranging. The ultrasonic sensors in this experiment are based on bionic science.


#### Parameters

Operating voltage: DC 5V
Operating current: 15 mA
Operating frequency: 40 KHz
Measuring angle: ≤ 15°C
Ultrasonic range: 2 cm ~ 400cm (peak)
Operating temperature: -10°C ~ +50°C
Output signal: analog signal
Dimensions of ultrasonic sensor: 20 x 45 x 12 mm (without pins)
Dimensions of ultrasonic sensor converter: 24 x 32 x 18 mm (without housing)
Positioning holes: diameter of 4.8 mm
Interface: telephone socket

#### Principle


After the ultrasonic sensor receives the trigger signal (the board sends a high level to the Trig pin), the module automatically sends eight 40KHZ square waves and starts timing.
The ultrasonic wave propagates in the air and immediately reflects back when it encounters the obstacle surface on the way (the Echo pin sends a high level to the board).
The ultrasonic receiver stops timing as soon as it receives the reflected waves. So then the module will record the interval between the initial transmission of the ultrasonic wave and the reception of the echo.
This is how the sensor calculate the distance from the obstacle.

**Working Principle:**
1. Trig pin enables the distance detection and transmits a high level signal for at least 10us.
2. Circularly send eight 40KHz square waves, and automatically detect whether there is a response signal.
3. If there is a response, Echo pin will output high lasting from ultrasonic wave transmitting to receiving.
Calculation of the distance between the ultrasonic sensor and the obstacle:
Distance = high level duration × 340m/s ÷ 2
(The sound speed varies from the nature and state of the medium. In air, the sound speed is about 340m/s at 1 standard atmosphere and 15 °C)
Since the ultrasonic module pins cannot be directly connected to the board, a converter module is required to connect it to the port in yellow on the board.

In this experiment, we connect the module to port 7. According to the board ports view, Trig pin is connected to io16 and Echo is to io35.

#### Wiring Diagram

#### Test Code
Open KidsBlock and connect the board to computer. Choose **File --> Load from your computer**.

Choose D:\Code\2.Code_kidsIOT and open **3.7Ultrasonic.sb3**.

Click  to connect to port and then .
#### Explanations

**Code Blocks**
| Blocks | Code block |
| :---------------------------: | :-----------------------------------: |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |

**Build blocks:**
Click  to add library **Ultrasonic**.

1. **Initialization**
Initialize the serial port.


2. **Main Code**
Loop: print the distance value detected by the ultrasonic sensor, and refresh the results every 0.1s.


#### Test Result

After uploading code, the serial monitor prints the distance value between the ultrasonic sensor and the obstacle.


---
### 3.8 Five-channel AD Button Module


When using, the five-channel AD button module only occupies one analog port, which greatly saves ports and pins. In analog acquisition, when we press different buttons, it outputs different voltages.

#### Parameters

Operating voltage: DC 3.3 ~ 5V
Operating current: 2.5 mA
Operating power: 0.0125 W
Operating temperature: -10°C ~ +50°C
Output signal: analog signal
Dimensions: 24 x 48 x 18 mm (without housing)
Positioning holes: diameter of 4.8 mm
Interface: telephone socket

#### Principle

Due to the circuit design, the corresponding output voltage varies when we press different button.
The voltage value is read by the board ADC and then compared with the pre-determined voltage range of each button, so that we can determine which button is pressed.


**What is ADC?**
ADC(Analog to Digital Converter) converts analog values to digital ones. The ADC acquisition is integrated in our board, so you can call it directly.
**Kidspico ADC Parameters**
1. Reference voltage: 3.3V
2. Resolution: 12bit
A n-bit ADC means this ADC contains 2ⁿ scales.
12bit ADC contains $2^{12}=4096$ scales, and it outputs totally 4096 digital values (including from 0~ 4095), each scale is $\frac{3.3}{4095}≈0.00081(V)$.
3. General ADC input voltage calculation:
$ Vin= \frac {AVDD_{ADC}}{2^{Resolution Bit}-1}*ReadData$
$AVDD_{ADC}$: Reference voltage

Modules with red housing are analog ones, so we should connect to analog io pins of the mainboard (ports with red).
In this experiment, we connect the module to port 8. According to the board ports view, the analog io pin at port 8 is io34.

#### Wiring Diagram

#### Test Code
Open KidsBlock and connect the board to computer. Choose **File --> Load from your computer**.

Choose D:\Code\2.Code_kidsIOT and open **3.8Five-way AD.sb3**.

Click  to connect to port and then .
#### Explanations

**Code Blocks**
| Blocks | Code block |
| :-------------------------------: | :-------------------------------------: |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |

**Conceive**
1. **Initialization**
Set pin mode and initialize the serial port.


2. **Main Code**
Loop: Determine which button us pressed.

**Build blocks:**
① Add blocks , , ,  and to the editing area as follows:

These blocks read the analog value of IO34, assign it to key_value, and then print it on the serial monitor.
② Add blocks , ,  and  as follows:

So far, when the ADC value read by pin IO34 is less than 500, the serial monitor print “ no key is pressed”. Note that there are two spaces in the front of the messages, so that the value will be separated from the printing contents.
③  is an "and" block. When the two conditions in it are both satisfied, it outputs True.
Add ,  and  as follows:

These conditions require the value at pin IO34 is within 500 ~ 1000.
Add  and  as follows:

When the ADC value at IO34 is within 500 ~ 1000, the serial monitor prints “ SW5 is pressed”. Note that there are two spaces in the front of the messages, so that the value will be separated from the printing contents.
④ Duplicate the "if" block in ③ four times.

Finally add a delay .

In the same way:
When the ADC value at IO34 is within 1000 ~ 2000, the serial monitor prints “ SW4 is pressed”.
When the ADC value at IO34 is within 2000 ~ 3000, the serial monitor prints “ SW3 is pressed”.
When the ADC value at IO34 is within 3000 ~ 4000, the serial monitor prints “ SW2 is pressed”.
When the ADC value at IO34 is within 4000 ~ 5000, the serial monitor prints “ SW1 is pressed”.

#### Test Result

NOTE: Before uploading code, please set baud rate first to avoid garbled words.
Click to set Buadrate to 115200.

